//
//  VREditViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/15.
//

import UIKit

class VREditViewController: VRBaseViewController, SAAnimationProtocol {

    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(20.font_bold)
        .alignment(.center).text("Edit Profile").end()
    private let signLabel          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold)
        .text("Signature").end()
    private lazy var signView       = c_make_ex(UITextView.self)
        .bgColor(.white.alpha(0.1)).corner(20).placeholderFont(16.font_medium)
        .color(.white).placeholderColor("#B2B2B2")
        .placeholder("Please enter your signature…")
        .placeholderInsets(UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)).end()
    private let bgLabel          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold)
        .text("Background").end()
    private let picButton = c_make_ex(UIButton.self)
        .image("Discover_pic").bgColor(.white.alpha(0.2))
        .corner(20).end()
    
    private let delButton = c_make_ex(UIButton.self)
        .image("Discover_del").end()
    private lazy var okButton = c_make_ex(UIButton.self)
        .color(.black).font(18.font_bold).corner(25)
        .text("Done").bgColor("#FFFC00").end()
    
    private let itemInfos: [VRProfileModel] = [.avatar,.name,.gender,.age]
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 60
        tableView.register(VREditCell.self)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
            signLabel
            signView
            bgLabel
            picButton.subviews{
                delButton
            }
            okButton
        }
        
        backButton.top(.statusHeight + 2).leading(5).size(40)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).height(240)
        signLabel.leading(15)
        signLabel.Top == tableView.Bottom + 20
        signView.leading(15).trailing(15).height(120)
        signView.Top == signLabel.Bottom + 15
        bgLabel.leading(15)
        bgLabel.Top == signView.Bottom + 20
        picButton.leading(15).size(150)
        picButton.Top == bgLabel.Bottom + 15
        delButton.top(8).trailing(8).size(30)
        okButton.width(310).height(50).centerHorizontally()
        okButton.Top == picButton.Bottom + 60
        
        signView.contentInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }

}

extension VREditViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        itemInfos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(VRSettingCell.self, indexPath: indexPath)
        cell.itemType = itemInfos[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        switch itemInfos[indexPath.row]{
        case .avatar:
            
            break
        case .name:

            break
        case .gender:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Terms of use"
//            webVC.webUrl = URLTool.technicalSupportUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
            self.showReportView(userId: "2222222")
            break
        case .age:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Privacy Policy"
//            webVC.webUrl = URLTool.privacyPolicyUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
            let ageView = VRChooseBrithdayView()
            saAnimate.animation(show: ageView)
            break
        default:
            return
        }
    }
    
    func showReportView(userId: String){
        
        let alertController = UIAlertController(title: nil,
                                message: nil, preferredStyle: .actionSheet)
        let vulgarAction = UIAlertAction(title: "Pornographic Vulgar", style: .default) { act in
//            self.reportAlertView(userId: userId)
        }
        let fraudAction = UIAlertAction(title: "Advertising Fraud", style: .default) { act in
//            self.reportAlertView(userId: userId)
        }
        let politAction = UIAlertAction(title: "Politically Sensitive", style: .default) { act in
//            self.reportAlertView(userId: userId)
        }
        let bloodyAction = UIAlertAction(title: "Bloody Violence", style: .default) { act in
//            self.reportAlertView(userId: userId)
        }
        let abuseAction = UIAlertAction(title: "Incite War And Abuse", style: .default) { act in
//            self.reportAlertView(userId: userId)
        }
        let othersAction = UIAlertAction(title: "Others", style: .default) { act in
//            self.reportAlertView(userId: userId)
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)

        cancelAction.setValue("#EA5A00".toColor, forKey: "titleTextColor")
        alertController.addAction(vulgarAction)
        alertController.addAction(fraudAction)
        alertController.addAction(politAction)
        alertController.addAction(bloodyAction)
        alertController.addAction(abuseAction)
        alertController.addAction(othersAction)
        alertController.addAction(cancelAction)

        self.present(alertController, animated: true)
    }
}

class VREditCell: UITableViewCell {

    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).end()
    let rightImage          = c_make_ex(UIImageView.self)
        .corner(20).bgColor(.red).end()
    let subLabel          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_medium).alignment(.right)
        .text("trehgbh").end()

    var itemType: VRProfileModel = .none {
        didSet {
            titleLabel.text = itemType.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            titleLabel
            rightImage
            subLabel
        }
        
        titleLabel.leading(15).centerVertically()
        rightImage.trailing(15).size(40).centerVertically()
        subLabel.trailing(15).centerVertically()
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
